package cn.beatuty.makeup.module.system.controller.admin.articlelike;

import cn.beatuty.makeup.module.system.controller.admin.articlelike.vo.ArticleLikePageReqVO;
import cn.beatuty.makeup.module.system.controller.admin.articlelike.vo.ArticleLikeRespVO;
import cn.beatuty.makeup.module.system.controller.admin.articlelike.vo.ArticleLikeSaveReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.articlelike.ArticleLikeDO;
import cn.beatuty.makeup.module.system.service.articlelike.ArticleLikeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.beatuty.makeup.framework.common.pojo.PageParam;
import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.common.pojo.CommonResult;
import cn.beatuty.makeup.framework.common.util.object.BeanUtils;
import static cn.beatuty.makeup.framework.common.pojo.CommonResult.success;

import cn.beatuty.makeup.framework.excel.core.util.ExcelUtils;

import cn.beatuty.makeup.framework.operatelog.core.annotations.OperateLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.beatuty.makeup.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 文章点赞")
@RestController
@RequestMapping("/app/article-like")
@Validated
public class ArticleLikeController {

    @Resource
    private ArticleLikeService articleLikeService;

    @PostMapping("/create")
    @Operation(summary = "创建文章点赞")
    public CommonResult<Long> createArticleLike(@Valid @RequestBody ArticleLikeSaveReqVO createReqVO) {
        return success(articleLikeService.createArticleLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章点赞")
    public CommonResult<Boolean> updateArticleLike(@Valid @RequestBody ArticleLikeSaveReqVO updateReqVO) {
        articleLikeService.updateArticleLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章点赞")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteArticleLike(@RequestParam("id") Long id) {
        articleLikeService.deleteArticleLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ArticleLikeRespVO> getArticleLike(@RequestParam("id") Long id) {
        ArticleLikeDO articleLike = articleLikeService.getArticleLike(id);
        return success(BeanUtils.toBean(articleLike, ArticleLikeRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得文章点赞分页")
    public CommonResult<PageResult<ArticleLikeRespVO>> getArticleLikePage(@Valid @RequestBody ArticleLikePageReqVO pageReqVO) {
        if (pageReqVO.getIsAllData()){
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ArticleLikeDO> pageResult = articleLikeService.getArticleLikePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleLikeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章点赞 Excel")
    @OperateLog(type = EXPORT)
    public void exportArticleLikeExcel(@Valid ArticleLikePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleLikeDO> list = articleLikeService.getArticleLikePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章点赞.xls", "数据", ArticleLikeRespVO.class,
                        BeanUtils.toBean(list, ArticleLikeRespVO.class));
    }

}